package com.zq.it.action.exam;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import com.zq.it.action.BaseAction;
import com.zq.it.dao.common.PageConfig;
import com.zq.it.pojo.Teacher;
import com.zq.it.pojo.exam.TCourseWare;
import com.zq.it.pojo.exam.data.ObjectData;
import com.zq.it.tool.Constant;
import com.zq.it.tool.Office2Swf;

/**
 * 文件管理(课件和资料管理)
 * 
 * @author 朱雪霖
 * @version $ID: CourseWareAction.java 2013-4-24 朱雪霖@$
 */
public class CoursewareAction extends BaseAction {

	/**
	 * 
	 */

	private static final long serialVersionUID = 1L;

	private Teacher teacher;
	private TCourseWare courseWare;
	private ObjectData tc;
	private Long[] ids;
	private List tlist;

	/**
	 * 打开课件列表界面
	 * 
	 * @return
	 */
	public String courseWarePare() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		PageConfig config = new PageConfig();
		config.setHql("from TCourseWare as t where t.TId =" + teacher.getId());
		config.setCountHql("select count(*) from TCourseWare as t where t.TId ="
				+ teacher.getId());
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("courseWare_courseWarePare.htm");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		tc = courseWareService.getListByPage(config);
		return "courseWareList";
	}

	/**
	 * 上传课件界面
	 * 
	 * @return
	 */
	public String addCourseWarePare() {
		return "addCourseWarePare";
	}

	/**
	 * 实现上传课件操作
	 * 
	 * @throws IOException
	 */

	public void addCourseWare() throws IOException

	{
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		courseWare.setFileName(getUploadFileName());
		courseWare.setTId(teacher.getId());

		String url = this.getUrl(courseWare); // 得到上传文件路径
		// 获取文件名
		String fileName = courseWare.getFileName().substring(0,
				courseWare.getFileName().lastIndexOf("."));
		String toolUrl = getRealPath("component"); // 获取绝对路径
		String readUrl = getRealPath("upload");

		try {
			if (courseWare.getType() == 1) {// 如果上传资料是可阅读,则转换成SWF文件
				upLoad(getUpload(), url + '/' + getUploadFileName());

				Office2Swf.ConvertSwf(toolUrl, readUrl,
						courseWare.getFileName(), fileName);
				courseWare.setFileName(fileName + ".swf");

				courseWareService.addCourseWare(courseWare);
				print_html_utf8("<script>parent.delResult('添加成功！')</script>");
			} else {
				upLoad(getUpload(), url + '/' + getUploadFileName());
				courseWareService.addCourseWare(courseWare);
				print_html_utf8("<script>parent.delResult('添加成功！')</script>");
			}
		} catch (Exception e) {
			e.printStackTrace();
			deleteFile(url + "/" + this.getUploadFileName());
			print_html_utf8("<script>parent.delResult('添加失败！')</script>");
		}
	}

	/**
	 * 学生在线阅读资料
	 */
	public String readCourseWare() {
		courseWare = courseWareService.getCourseWare(ids[0]);
		String url = "upload/courseWare/readOnline/" + courseWare.getFileName();
		getHttpSession().setAttribute("readUrl", url);
		return "readUrl";
	}

	/**
	 * 下载课件
	 */
	public void downloadCourseWare() {
		courseWare = courseWareService.getCourseWare(ids[0]);
		String url = this.getUrl(courseWare);
		if (courseWare != null || courseWare.equals("")) {
			download(
					courseWare.getName() + courseWare.getFileName().substring(
							courseWare.getFileName().lastIndexOf("."),
							courseWare.getFileName().length()),
					url + "/" + courseWare.getFileName());
		}
	}

	/**
	 * 删除资料
	 * 
	 * @throws IOException
	 */
	public void deleteCourseWare() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		try {
			courseWareService.deleteCourseWare(ids,
					getRealPath(Constant.COURSEWARE), teacher);
			print_text_utf8("ok");
		} catch (Exception e) {
			e.printStackTrace();
			print_text_utf8("false");
		}
	}

	/**
	 * 多条件查询资料
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String searchCourseWare() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		String hql = null;
		if (!courseWare.getName().isEmpty()) {
			courseWare.setName(
					URLDecoder.decode(courseWare.getName(), Constant.UTF_8));
		}
		if (!courseWare.getType().equals("-1")) {
			courseWare.setType(courseWare.getType());
		}
		if (courseWare.getName().equals("") && courseWare.getType() == -1) {
			hql = "from TCourseWare as model where 1=1";
		} else {
			hql = courseWareService.createCourseWareHql(courseWare);
		}
		PageConfig config = new PageConfig();
		config.setHql(hql);
		config.setCountHql(
				"select count(*) " + hql + "and model.TId=" + teacher.getId());
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("courseWare!searchCourseWare.htm");
		config.setExtendUrl("courseWare.name="
				+ URLEncoder.encode(
						URLEncoder.encode(courseWare.getName(), Constant.UTF_8),
						Constant.UTF_8)
				+ "&courseWare.type=" + courseWare.getType()
				+ "&courseWare.TId=" + courseWare.getTId());
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		tc = courseWareService.getListByPage(config);
		return "courseWareList";
	}

	/**
	 * 获取文件url
	 * 
	 * @param courseWare
	 * @return
	 */

	public String getUrl(TCourseWare courseWare) {
		String url = null;
		if (courseWare.getType() == 0) {
			url = Constant.COURSEWARE + "kejian";
		} else if (courseWare.getType() == 1) {
			url = Constant.COURSEWARE + "readOnline";
		}
		return url;
	}

	/**
	 * setters,getters方法
	 * 
	 * @return
	 */

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public TCourseWare getCourseWare() {
		return courseWare;
	}

	public void setCourseWare(TCourseWare courseWare) {
		this.courseWare = courseWare;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public ObjectData getTc() {
		return tc;
	}

	public void setTc(ObjectData tc) {
		this.tc = tc;
	}

	public List getTlist() {
		return tlist;
	}

	public void setTlist(List tlist) {
		this.tlist = tlist;
	}

}
